const Router = require("@koa/router"); // 引入路由中间件
const router = new Router() // 新建一个路由对象

const studentAPI = require("./studentsAPI") // 导入student的操作API

const koaBody = require('koa-body'); //引入koa-body

// 首页
router.get("/students",async (ctx, next) => {
    let students = await studentAPI.getAllStudents()
    if(!students)
        return ctx.response.body = "学生信息未找到"
    ctx.render("index", {
        fruits: [
            "香蕉",
            "苹果",
            "橘子",
            "西瓜"
        ],
        students
    })
    await next();
})
// 添加页面
router.get("/students/new",async (ctx, next) => {
    ctx.render("new")
})
// 处理添加学生请求
router.post("/students/new",koaBody(), async (ctx, next) => {
    let obj = ctx.request.body;
    obj.id = 122;
    let msg = await studentAPI.addStudent(obj);
    if(msg === 'success'){
        return ctx.redirect("/students")
    }
    console.log(msg)

})
// 渲染编辑页面
router.get("/students/edit",async (ctx, next) => {
})
// 处理编辑请求
router.post("/students/edit",async (ctx, next) => {
})
// 处理删除请求
router.get("/students/delete",async (ctx, next) => {
})
exports.router = router;